//
//  CaseManagementViewController.m
//  FastMedical
//
//  Created by 李通 on 16/7/6.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "CaseManagementViewController.h"
#import "CaseHeadView.h"
#import "MeicalModel.h"
#import "CaseCell.h"
#import "AddCaseController.h"
#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])
@interface CaseManagementViewController ()<UITableViewDataSource,UITableViewDelegate,AddCaseControllerDelegate>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)CaseHeadView *headView;
@property (nonatomic,weak)UIButton *addButton;
@property (nonatomic,strong)NSMutableArray<MeicalModel*> *CaseArray;
@end

@implementation CaseManagementViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}
#pragma mark - 懒加载
-(NSMutableArray<MeicalModel*>*)CaseArray{
    if (!_CaseArray) {
        _CaseArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"array.data")];
        if(!_CaseArray){
            _CaseArray = [NSMutableArray array];
        }
    }
    return _CaseArray;
}

#pragma  mark - UI
-(void)setupUI{
    [self setupHeadView];
    [self setupTableView];
    [self setupAddButton];
}
-(void)setupHeadView{
    CaseHeadView *headView = [[CaseHeadView alloc]init];
    self.headView = headView;
    [self.view addSubview:headView];
}
-(void)setupTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
//    [self.tableView registerClass:[CaseCell class] forCellReuseIdentifier:@"cell"];
}
-(void)setupAddButton{
    UIButton *addButton = [[UIButton alloc]init];
    self.addButton = addButton;
    addButton.backgroundColor = kThemeColor;
    [addButton setTitle:@"添加病例" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:addButton];
    [addButton addTarget:self action:@selector(didClickButton) forControlEvents: UIControlEventTouchUpInside];
}
#pragma mark - 点击button
-(void)didClickButton{
    AddCaseController *vc = [[AddCaseController alloc]init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - massonry
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.offset(0);
        make.right.offset(0);
        make.height.mas_equalTo(150);
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.left.offset(10);
        make.right.offset(-10);
        make.bottom.offset(-10);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headView.mas_bottom).offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.offset(-60);
    }];
}
#pragma mark - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.CaseArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeicalModel *model = self.CaseArray[indexPath.row];
    CaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil){
        
        cell = [[CaseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
        cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

   //点击屏幕回退键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark  -addCaseControllerDelegate
-(void)addCaseController:(AddCaseController *)addCaseVc WithModel:(MeicalModel *)model{
    [self.CaseArray addObject:model];
    [self.tableView reloadData];
    [NSKeyedArchiver archiveRootObject:self.CaseArray toFile:kFilePath(@"array.data")];
}
@end
